package com.leruan.aasimp.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.bean.UsedCarSaleMainBean;
import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.UsedCarSaleDao;
import com.leruan.aasimp.entity.UsedCarSale;

public class UsedCarSaleDaoImpl extends HibernateDaoSupport implements
		UsedCarSaleDao {

	public List<UsedCarSaleMainBean> getUsedCarSaleList(
			Map<String, Object> map, int pageNo) throws Exception {

		Session session = this.getSession();

		StringBuffer sb = new StringBuffer(
				"select t1.id, t1.title, t1.intro, t1.buyDate, t1.mileage, t1.money, t1.mainImg, t2.dispalcement from UsedCarSale t1,CarBasicInfo t2 where t1.carBasicId = t2.id ");
		if (map.get("brand") != null)
			sb.append(" and t2.brand=:brand ");
		if (map.get("department") != null)
			sb.append(" and t2.department=:department ");
		if (map.get("startPrice") != null)
			sb.append(" and t1.money>=:startPrice ");
		if (map.get("endPrice") != null)
			sb.append(" and t1.money<=:endPrice ");
		if (map.get("startCarYear") != null)
			sb.append(" and t1.buyDate<=:startCarYear ");
		if (map.get("endCarYear") != null)
			sb.append(" and t1.buyDate>=:endCarYear ");

		sb.append(" order by t1.releaseTime desc ");
		Query query = session.createQuery(sb.toString());

		if (map.get("brand") != null)
			query.setString("brand", map.get("brand") + "");
		if (map.get("department") != null)
			query.setString("department", map.get("department") + "");
		if (map.get("startPrice") != null)
			query.setInteger("startPrice", (Integer) map.get("startPrice"));
		if (map.get("endPrice") != null)
			query.setInteger("endPrice", (Integer) map.get("endPrice"));
		if (map.get("startCarYear") != null)
			query.setDate("startCarYear", (Date) map.get("startCarYear"));
		if (map.get("endCarYear") != null)
			query.setDate("endCarYear", (Date) map.get("endCarYear"));

		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE);

		List list = query.list();

		List<UsedCarSaleMainBean> usedCarList = new ArrayList<UsedCarSaleMainBean>();

		for (int i = 0; i < list.size(); i++) {
			UsedCarSaleMainBean uBean = new UsedCarSaleMainBean();
			Object[] object = (Object[]) list.get(i);
			uBean.setId(Long.parseLong(object[0].toString()));
			uBean.setTitle((String) object[1]);
			uBean.setIntro((String) object[2]);
			uBean.setBuyDate(((Date) object[3]));
			uBean.setMileage((String) object[4]);
			uBean.setMoney((String) object[5]);
			uBean.setImageUrl((String) object[6]);
			uBean.setDispalcement(object[7].toString());
			usedCarList.add(uBean);
		}
		return usedCarList;
	}

	public Long getUsedSaleTotal(Map<String, Object> map) throws Exception {

		Session session = this.getSession();
		StringBuffer sb = new StringBuffer(
				"select count(t1.id) from UsedCarSale t1,CarBasicInfo t2 where t1.carBasicId = t2.id ");
		if (map.get("brand") != null)
			sb.append(" and t2.brand=:brand ");
		if (map.get("department") != null)
			sb.append(" and t2.department=:department ");
		if (map.get("startPrice") != null)
			sb.append(" and t1.money>=:startPrice ");
		if (map.get("endPrice") != null)
			sb.append(" and t1.money<=:endPrice ");
		if (map.get("startCarYear") != null)
			sb.append(" and t1.buyDate<=:startCarYear ");
		if (map.get("endCarYear") != null)
			sb.append(" and t1.buyDate>=:endCarYear ");

		Query query = session.createQuery(sb.toString());

		if (map.get("brand") != null)
			query.setString("brand", map.get("brand") + "");
		if (map.get("department") != null)
			query.setString("department", map.get("department") + "");
		if (map.get("startPrice") != null)
			query.setInteger("startPrice", (Integer) map.get("startPrice"));
		if (map.get("endPrice") != null)
			query.setInteger("endPrice", (Integer) map.get("endPrice"));
		if (map.get("startCarYear") != null)
			query.setDate("startCarYear", (Date) map.get("startCarYear"));
		if (map.get("endCarYear") != null)
			query.setDate("endCarYear", (Date) map.get("endCarYear"));

		return (Long) query.uniqueResult();
	}

	public UsedCarSale getSaleInfoById(String id) throws Exception {
		Long carBasicId = Long.parseLong(id);
		Session session = this.getSession();
		String hql = "from UsedCarSale where id = ?";
		return (UsedCarSale) session.createQuery(hql).setLong(0, carBasicId)
				.uniqueResult();
	}

	public List<String> getImagesUrlById(String id) throws Exception {
		Long carSaleId = Long.parseLong(id);
		Session session = this.getSession();
		String hql = "select imageAddress from CarSaleImage where carSaleId = ?";
		List<String> list = (List<String>) session.createQuery(hql).setLong(0, carSaleId).list();
		return list;
	}

	public void updateUsedCarSale(UsedCarSale usedCarSale) throws Exception {
		Session session = this.getSession();
		session.update(usedCarSale);
	}

	public List<UsedCarSaleMainBean> getUsedCarSaleListById(String userId)
			throws Exception {
		Session session = this.getSession();
		String hql = "select t1.id, t1.title, t1.intro, t1.buyDate, t1.mileage, t1.money, t1.mainImg, t2.dispalcement from UsedCarSale t1,CarBasicInfo t2 where t1.carBasicId = t2.id and t1.userId = ? order by t1.releaseTime desc ";
		
		Query query = session.createQuery(hql);
		
		List list = query.setString(0, userId).list();
		List<UsedCarSaleMainBean> usedCarList = new ArrayList<UsedCarSaleMainBean>();

		for (int i = 0; i < list.size(); i++) {
			UsedCarSaleMainBean uBean = new UsedCarSaleMainBean();
			Object[] object = (Object[]) list.get(i);
			uBean.setId(Long.parseLong(object[0].toString()));
			uBean.setTitle((String) object[1]);
			uBean.setIntro((String) object[2]);
			uBean.setBuyDate(((Date) object[3]));
			uBean.setMileage((String) object[4]);
			uBean.setMoney((String) object[5]);
			uBean.setImageUrl((String) object[6]);
			uBean.setDispalcement(object[7].toString());
			usedCarList.add(uBean);
		}
		return usedCarList;
	}

	public void deleteMyUsedCarSale(String id, String userId) throws Exception {
		Session session = this.getSession();
		String hql = "delete UsedCarSale where id = ? and userId = ? ";
		session.createQuery(hql).setString(0, id).setString(1, userId).executeUpdate();
	}

	public void saveUsedCarSale(UsedCarSale usedCarSale) throws Exception {
		Session session = this.getSession();
		session.save(usedCarSale);
	}

	public Long findId(UsedCarSale usedCarSale) throws Exception {
		Session session = this.getSession();
		UsedCarSale usedCarSale1 = (UsedCarSale) session.get(UsedCarSale.class, usedCarSale);
		return usedCarSale1.getId();
	}

}
